
$(function() {
	
	// Accordion
	$("#accordion").accordion({
		header : "h3",
		fillSpace: true
	});
	$("#milaccordion").accordion({
		header : "h3",
		fillSpace: true

	});

	// Tabs
	$('#tabs').tabs();

	// Buttons
	$('.buttonRes').button();
	$('.buttonResRem').button();
	$('.buttonMil').button();
	$('.buttonx').button();
	$('#xstats').button();
	$('.buttonclose').button();
	$('.genbutton').button();
	$(".tbutton").button();
	// Carousel



	$("#missionsPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 550,
		width : 700,
		show: 'blind',
		autoOpen : false
	});
	
	
	$("#statsPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 550,
		width : 700,
		show: 'blind',
		autoOpen : false
	});
	
	
	$("#marketPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 600,
		width : 700,
		show: 'blind',
		modal: true,
		autoOpen : false
	});
	
	$("#researchPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 550,
		width : 700,
		modal: true,
		show: 'blind',
		title : 'Research Laboratory',
		autoOpen : false
	});
	
	$("#alliesPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 550,
		width : 700,
		modal: true,
		show: 'blind',
		title : 'My Allies',
		autoOpen : false
	});
	
	$("#messagesPopup").dialog({
		buttons : {
			"Close" : function() {
				$(this).dialog("close");
			}
		},
		height : 550,
		width : 700,
		modal: true,
		show: 'blind',
		hide: 'blind',
		title : 'My Messages',
		autoOpen : false
	});
	

	$("#levelPopup").dialog({
		resizable: false,
		height:140,
		modal: true,
		autoOpen : false,
		buttons: {
			'Upgrade level': function() {
		if(upgradelevel())
			{
				$(this).dialog('close');
			}
		else
			{
			jalert("Insufficient Money to upgrade level",2)
			}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});

	
	$('#tabs').bind('tabsshow', function(event, ui) {
		if(!isGoogleLoaded)
		{
		//FIXME: Show loading image...
			return;
		}
		
if (ui.panel.id == "tabsTerritory") {
 initializeMap();
} else if (ui.panel.id == "tabsWar") {
initializeWarMap();
	}
	else if(ui.panel.id=="tabsCivilian"){
		changeIdleResource(totalRes);
	}

});

	$('#xstats').bind('click', function(event) {
			showMiniLoading();  
		$.get('stats.jsp', function(data) {
			$('#statsPopup').dialog('open');
			$('#statsPopup').html(data);
			 hideLoading(); 
			});

		
	});
	
	
	$('#marketbtn').bind('click', function(event) {
		if(yahooInited==false)
			{
		jalert("Please wait for your application to finish loading..",2); 
		return;
			}
		showMiniLoading();
		$.get('/market', function(data) {
			$('#marketPopup').dialog('open');
			$('#marketPopup').html(data);
			  hideMiniLoading(); 
			});
	
	});
	
	
	$('#researchbtn').bind('click', function(event) {
		
		if(yahooInited==false)
		{
	jalert("Please wait for your application to finish loading..",2); 
	return;
		}
		showMiniLoading();
	$.get('/science', function(data) {
		$('#researchPopup').dialog('open');
		$('#researchPopup').html(data);
		  hideMiniLoading(); 
		});
		
		
	});
	
	
	$('#alliesbtn').bind('click', function(event) {
		
		if(yahooInited==false)
		{
	jalert("Please wait for your application to finish loading..",2); 
	return;
		}
		showMiniLoading();
	$.get('/getallallies', function(data) {
		$('#alliesPopup').dialog('open');
		$('#alliesPopup').html(data);
		  hideMiniLoading(); 
		});
		
		
	});
	

	$('#messagesbutton').bind('click', function(event) {
		$('#messagesPopup').dialog('open');
		$("#msgtext").html("");
	});
	
	
	$('.buttonRes').bind('click', function(event) {
		// FIXME: Make this 5 if shift is held down
			var addNumber = 1;
			addResource(event.currentTarget.id, addNumber);
		});

	$('.buttonResRem').bind('click', function(event) {
		var addNumber = 1;
		remResource(event.currentTarget.id, addNumber);
	});

	$('.buttonMil').bind('click', function(event) {
		var addNumber = 1;
		addMilResource(event.currentTarget.id, addNumber);
	});

	$("#jalert").dialog({
		modal : true,
		autoOpen : false,
		show : 'blind',
		hide : 'explode',
		buttons : {
			Ok : function() {
				$(this).dialog('close');
			}
		}
	});




 $(".territoryCostTable tr").mouseover(function() {
		$(this).addClass("statsover");
	}).mouseout(function() {
		$(this).removeClass("statsover");
	});
	
	$(".territoryCostTable tr:even").addClass("statsalt");
	$('#addloc').button();
	
	$('#jalert').ajaxError(function(e, xhr, settings, exception) {
		//jalert('OOPS! An error occured during your last action.  ' + e + ' for ' + xhr + ' settings=' + settings + ' exception ' + exception,3);
		});
	
	$('#addloc').bind('click',function(event) {
						if (!oktobuildbase)
							{
							jalert("You have insufficient resources to build a new base!",2)
							return;
							}
						showMiniLoading();	
	$.getJSON("addpersonlocation/add",
							{
							lat : newlat,
							lon : newlon,
							terdistance:terdistance
							},
							function(rtnmsg) {
							
								if (null == rtnmsg) {
                                jalert("Unable to contact server! Please reconnect! Any changes you make will be lost",2)
                                return;
                                }
								//FIXME check the return code 
								jalert("You have successfully added the new location",4);
								syncServer(true);
								//FIXME. Add location on client side instead of whole refresh
								showLocations(map,true);
								hideMiniLoading();
							});
	
						});
	
});




function jalert(msg, mode) {
	var preicon = "images/alerts/stop.png";
	switch (mode) {
		case 0 :
			preicon = "images/alerts/ok.png";
			break;
		case 1 :
			preicon = "images/alerts/warn.png";
			break;
		case 2 :
			preicon = "images/alerts/stop.png";
			break;
		case 3 :
			preicon = "images/alerts/oh.png";
			break;
		//location added
		case 4:
			preicon="images/alerts/locadded.png"
				break;
		case 5:
			preicon="images/alerts/armydeployed.png"
				break;
		case 99 :
			preicon = "images/alerts/unknown.png";
			break;
		default :
			preicon = "images/alerts/unknown.png";
			break;

	}

	var prepend = "<img src=" + preicon + "><br>";
	$('#jalert').html(prepend + "<p class='mesg'>"+ msg + "</p>");
	$('#jalert').dialog('open');
}


function upgradelevel()
{
return false;	
}